package com.loong.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.framework.constant.SystemConst;
import com.loong.framework.exception.RRException;
import com.loong.framework.utils.CommonUtil;
import com.loong.framework.utils.SystemUtil;
import com.loong.system.dto.RoleAddDTO;
import com.loong.system.dto.RolePageDTO;
import com.loong.system.dto.RoleUpdateDTO;
import com.loong.system.entity.Role;
import com.loong.system.mapper.RoleMapper;
import com.loong.system.service.IRoleService;
import com.loong.system.vo.RoleListVO;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * @author 47349
 * @description 针对表【sys_role(角色表)】的数据库操作Service实现
 * @createDate 2024-04-26 00:49:31
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
  @Override
  public Page<RoleListVO> pageRoleList(RolePageDTO rolePageDTO) {
    String roleName = rolePageDTO.getRoleName();
    String roleSign = rolePageDTO.getRoleSign();

    Page<Role> page = SystemUtil.buildPageFromDTO(rolePageDTO, Role.class);

    page =
        this.lambdaQuery()
            .like(CommonUtil.notNullOrEmpty(roleName), Role::getRoleName, roleName)
            .like(CommonUtil.notNullOrEmpty(roleSign), Role::getRoleSign, roleSign)
            .orderByDesc(Role::getGmtCreate)
            .page(page);

    return SystemUtil.buildResource(page, RoleListVO.class);
  }

  @Override
  public void removeRole(String roleId) {
    Role role = this.getById(roleId);
    if (CommonUtil.isNullOrEmpty(role)) {
      throw new RRException("角色不存在");
    }
    if (SystemConst.CanNotDelRoleList.contains(role.getRoleSign())) {
      throw new RRException("该角色不允许删除");
    }
    this.removeById(roleId);
  }

  @Override
  public void checkRole(RoleAddDTO roleAddDTO) {
    Long count =
        this.lambdaQuery()
            .eq(Role::getRoleName, roleAddDTO.getRoleName())
            .or()
            .eq(Role::getRoleSign, roleAddDTO.getRoleSign())
            .count();
    if (count > 0) {
      throw new RRException("角色名或角色标识已存在");
    }
  }

  @Override
  public void updateRole(RoleUpdateDTO roleUpdateDTO) {
    Role queryRole = this.getById(roleUpdateDTO.getId());
    if (SystemConst.CanNotDelRoleList.contains(queryRole.getRoleSign())) {
      throw new RRException("该角色不允许修改");
    }

    Role role = SystemUtil.buildResource(roleUpdateDTO, Role.class);
    this.updateById(role);
  }

  @Override
  public Map<String, String> roleIds2RoleNameMap(List<String> roleIds) {
    if (CommonUtil.isNullOrEmpty(roleIds)) {
      return Map.of();
    }
    List<Role> roleList = this.listByIds(roleIds);
    if (CommonUtil.isNullOrEmpty(roleList)) {
      return Map.of();
    }
    return roleList.stream().collect(Collectors.toMap(Role::getId, Role::getRoleName));
  }

  @Override
  public List<String> getRoleIdBySign(List<String> roleSigns) {
    if (CommonUtil.isNullOrEmpty(roleSigns)) {
      return List.of();
    }
    List<Role> roleList = this.lambdaQuery().in(Role::getRoleSign, roleSigns).list();
    if (CommonUtil.isNullOrEmpty(roleList)) {
      return List.of();
    }
    return roleList.stream().map(Role::getId).collect(Collectors.toList());
  }

  @Override
  public List<String> getRoleSignsByRoleIds(List<String> roleIds) {
    if (CommonUtil.isNullOrEmpty(roleIds)) {
      return List.of();
    }
    List<Role> roleList = this.listByIds(roleIds);
    if (CommonUtil.isNullOrEmpty(roleList)) {
      return List.of();
    }
    return roleList.stream().map(Role::getRoleSign).collect(Collectors.toList());
  }
}
